1778A - Flip Flop Sum - CodeForces Solution


greedy implementation

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define ll  long long
#define sz(x) (int)x.size()
#define fi first
#define sc second
#define pb push_back
#define yes cout<<'YES'<<endl;
#define no cout<<'NO'<<endl;
#define all(a) (a).begin(),(a).end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b))
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
using namespace std;

int binarysearch(int arr[],int size ,int key){
int start =0;
int end =size-1;
int mid=(start+end)/2;
while (start<=end)
{
if (arr[mid]==key)
{
return mid;
}
else if (arr[mid]<key)
{
start=mid+1;
}
else{
end=mid-1;
}
mid=(start+end)/2;
}
return -1;
 }

int factorial(int n) {
if(n > 1)
 return n * factorial(n - 1);
else
return 1;
}

int main(){
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
    ll n;
    cin>>n;
    ll sum=0,v=0;
    ll arr[n],p=0;
    for (int i = 0; i < n; i++)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    for (int i = 1; i < n; i++)
    {
        if (arr[i-1]==-1 && arr[i]==-1)
        {
            
            v=1;
            break;
        }
        else if (arr[i]==-1 || arr[i-1]==-1)
        {
            p=1;
        }
        else
        {
            v=0;
        }
        
        
    }
    if (v==1)
    {
        cout<<sum+4<<endl;
    }
    else if (p==0)
    {
        cout<<sum-4<<endl;
    }
    else
    {
        cout<<sum<<endl;
    }
    
    
}
    return 0;
}


Comments

Submit
0 Comments
More Questions

3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret